package com.mc.moring.main;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.http.SslError;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.mc.moring.R;
import com.mc.moring.app.LogUtils;
import com.mc.moring.base.BaseActivity;
import com.mc.moring.base.HttpCallback;
import com.mc.moring.base.SaveZiLiPageTool;
import com.mc.moring.bean.WebPageContentBean;
import com.mc.moring.bean.getWebLinkConfigBean;
import com.mc.moring.login.LoginActivity;
import com.mc.moring.main.modle.PermisosModle;
import com.mc.moring.util.AppManager;
import com.mc.moring.util.GsonUtils;
import com.mc.moring.util.PointMgr;
import com.mc.moring.util.SharePreUtil;
import com.mc.moring.widge.MyDialog;
import com.mc.moring.widge.HeaderViewLayout;
import com.mc.moring.widge.NewWebView;

import java.util.HashMap;
import java.util.Map;

public class PrivacyActivity extends BaseActivity {

    HeaderViewLayout headerTitle;
    TextView mTv3;
    TextView mTvDiscrepar;
    TextView mTvConfirmar;
    LinearLayout mLlConfirmar;
    NewWebView webview;
    MyDialog myDialog;
    String webContent = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PointMgr.INSTANCE.sendEvent("cl_app_plata_6");
        Log.e("PointMgr.INSTANCE.sendEvent","cl_app_plata_6");
        setContentView(R.layout.activity_privacy);
        SharedPreferences sharedPreferences = getSharedPreferences("goPermisos", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("goPermisos","false");
        editor.apply();
        init();
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public void initView() {
        setImmersionBarTextDark(headerViewLayout, true, 0);
        headerTitle = findViewById(R.id.header_title);
        mTv3 = findViewById(R.id.text3);
        mTvDiscrepar = findViewById(R.id.tv_discrepar);
        mTvConfirmar = findViewById(R.id.tv_confirmar);
        mLlConfirmar = findViewById(R.id.ll_confirmar);
        webview = findViewById(R.id.webview);
        mTv3.setText("Por favor, deslizar lea todo política de privacidad");
        mTvDiscrepar.setText("Discrepar");
        mTvConfirmar.setText("Confirmar");
        myDialog = new MyDialog(this);

        if ( webContent != null && !webContent.isEmpty() ){
            webview.loadData(webContent, "text/html","UTF-8");
            PointMgr.INSTANCE.sendEvent("cl_app_plata_7");
            Log.e("PointMgr.INSTANCE.sendEvent","cl_app_plata_7");
        }
        myDialog.setDialogViewOnClickListener(new MyDialog.DialogViewOnClickListener() {
            @Override
            public void onClickConfirm() {
                Intent intent = new Intent(PrivacyActivity.this, MainActivity.class);
                startActivity(intent);
                getActivity().finish();
            }

            @Override
            public void onClickCancel() {
                Intent intent = new Intent(PrivacyActivity.this, MainActivity.class);
                startActivity(intent);
                getActivity().finish();
                AppManager.getAppManager().finishActivity(PermisosActivity.class);
            }
        });
        mTvDiscrepar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharePreUtil.saveStringData("DoNotShowAgain","TRUE");
                PointMgr.INSTANCE.sendEvent("cl_app_plata_9");
                Log.e("PointMgr.INSTANCE.sendEvent","cl_app_plata_9");
                myDialog.showToPermissionSettingDialog(PrivacyActivity.this, "PrivacyActivity");
            }
        });

        mTvConfirmar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PointMgr.INSTANCE.sendEvent("cl_app_plata_8");
                Log.e("PointMgr.INSTANCE.sendEvent","cl_app_plata_8");
                String privacyClick = new SaveZiLiPageTool(PrivacyActivity.this).getKeyValue("privacyClick");
                if (TextUtils.isEmpty(privacyClick))return;
                new SaveZiLiPageTool(PrivacyActivity.this).puttKeyValue("privacy", "privacy");
                Intent intent = new Intent(PrivacyActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view,
                                           SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webview.setOnNewWebViewScroolChangeListener(new NewWebView.ScrollInterface() {
            @Override
            public void onSChanged(int l, int t, int oldl, int oldt) {
                float webcontent = webview.getContentHeight() * webview.getScale();//webview的高度
                if (webcontent == 0) {
                    return;
                }
                if (webview.getHeight() + webview.getScrollY() >= webcontent - 10) {
                    new SaveZiLiPageTool(PrivacyActivity.this).puttKeyValue("privacyClick", "1");
                    mTvConfirmar.setBackgroundResource(R.drawable.bg_gradient_b4ffcc_76efff_corner_8);
                }
            }
        });

    }

    private void loadPrivacyByWebLinkConfig() {
        Map<String, String> map = new HashMap<>();
        PermisosModle.Companion.getInstant().getZiLiPageWebLinkConfig(map, new HttpCallback() {
            @Override
            public void onSuccess(@NonNull String data) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        getWebLinkConfigBean getWebLinkConfig = GsonUtils.INSTANCE.getGson().fromJson(data, getWebLinkConfigBean.class);
                        if (getWebLinkConfig != null) {
                            try{
                                String privacyPolicy = getWebLinkConfig.getData().getPrivacyAgree();
                                LogUtils.logE("private policy>" + privacyPolicy);
                                webview.loadUrl(privacyPolicy);
                                PointMgr.INSTANCE.sendEvent("cl_app_plata_7");
                                Log.e("PointMgr.INSTANCE.sendEvent","cl_app_plata_7");
                            } catch (Exception e){
                                e.printStackTrace();
                            }

                        }
                    }
                });
                hideLoading();
            }

            @Override
            public void onFaill(@NonNull String e) {
                hideLoading();
            }
        });
    }

    @Override
    public void initData() {
        Map<String, String> webContentMap = new HashMap<>();
        webContentMap.put("pageType","privacyPolicy");
        PermisosModle.Companion.getInstant().getWebPageContent(webContentMap, new HttpCallback() {
            @Override
            public void onSuccess(@NonNull String data) {
                LogUtils.logE("getWebPageContent privacyPolicy---data:"+data);
                WebPageContentBean webPageContentBean = GsonUtils.INSTANCE.getGson().fromJson(data, WebPageContentBean.class);
                if (webPageContentBean.getCode().equals("0000") && !TextUtils.isEmpty(webPageContentBean.getData())) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            webContent = webPageContentBean.getData();
                            if ( webview != null ){
                                webview.loadData(webContent, "text/html","UTF-8");
                                PointMgr.INSTANCE.sendEvent("cl_app_plata_7");
                                Log.e("PointMgr.INSTANCE.sendEvent","cl_app_plata_7");
                            }

                        }
                    });
                } else {
                    loadPrivacyByWebLinkConfig();
                }
                hideLoading();
            }

            @Override
            public void onFaill(@NonNull String e) {
                hideLoading();
            }
        });
    }

    @Override
    public void onBackPressed() {
        myDialog.showToPermissionSettingDialog(PrivacyActivity.this, "PrivacyActivity");
    }

}
